import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import os

'''
获取目录下的所有文件名（全名）
'''
def getFilenameList(direction):
    namelist=[]
    for filename in os.listdir(direction):
        namelist.append(os.path.join(direction,filename))
        # print(direction+filename)
    # print(type(namelist))
    return namelist
def readImages(filenameList):
    imageList = []
    with tf.Session() as sess:
        for filename in filenameList:
            # print(filename)
            # 读取图像的原始数据
            image_raw_data = tf.gfile.FastGFile(filename,'rb').read()  # 必须是 ‘rb’ 模式打开，否则会报错
            # 将图像使用 jpeg 的格式解码从而得到图像对应的三维矩阵
            # tf.image.decode_jpeg 函数对 png 格式的图像进行解码。解码之后的结果为一个张量，
            # 在使用它的取值之前需要明确调用运行的过程。
            print(filename)
            img_data = tf.image.decode_jpeg(image_raw_data)
            # arr = np.reshape(img_data.eval(sess), [-1])  # 多维矩阵转一维矩阵
            arr = sess.run(tf.reshape(img_data.eval(), [-1]))
            imageList.append(arr)
    return np.array(imageList)

filework = 'D:/images/opencv/image_9000/'
# filework = r'D:\python\workspace\ideaTest\venv\images'
a = getFilenameList(filework)
print(np.shape(a))
b = readImages(a)
# print(np.shape(b))